/*
 * 项目名称:    WeChatRobot
 * 创建人员:    DongPeng
 * 创建时间:    2022-12-02
 * copyright(c) 2022 无锡星际智慧物流有限公司
 */
package system;

import cn.hutool.core.util.StrUtil;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.File;

/**
 * java fx界面
 *
 * @author DongPeng
 * @version 1.0.0
 * @date 2022-12-02
 * @description
 */
public class Main extends Application {

    public static void main(String[] args) throws InterruptedException {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        System.out.println("------------------------连发器开始运行------------------------");
        //确认按钮
        Button yes = new Button("确认");
        //发送样式单选
        RadioButton textButton = new RadioButton("发送文本");
        RadioButton imageButton = new RadioButton("发送图片");
        ToggleGroup radioGroup = new ToggleGroup();
        textButton.setSelected(true);
        textButton.setToggleGroup(radioGroup);
        imageButton.setToggleGroup(radioGroup);

        //绑定容器
        GridPane pane = new GridPane();
        pane.add(new Label("请选择发送样式:"), 0, 0);
        pane.add(textButton, 1, 1);
        pane.add(imageButton, 1,2);
        pane.add(yes, 2, 3);
        Scene scene = new Scene(pane, 300, 100);
        primaryStage.setScene(scene);
        primaryStage.getIcons().add(new Image("icon/pic.png"));
        primaryStage.setTitle("微信连发器");
        primaryStage.initStyle(StageStyle.DECORATED);
        //文本容器
        GridPane textPane = runText(primaryStage, scene);
        //图片容器
        GridPane imagePane = runImage(primaryStage, scene);
        Scene textScene = new Scene(textPane, 650, 300);
        Scene imageScene = new Scene(imagePane, 650, 300);
        yes.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //点击后执行的函数
                if (textButton.isSelected()) {
                    primaryStage.setScene(textScene);
                    primaryStage.show();
                }
                if (imageButton.isSelected()) {
                    primaryStage.setScene(imageScene);
                    primaryStage.show();
                }
            }
        });
        primaryStage.show();
    }

    public GridPane runText(Stage primaryStage, Scene scene) {
        Button send = new Button("发送");
        Button canel = new Button("返回");
        //文本框
        Label text = new Label("请确保微信聊天界面处于最上层!!!");
        text.setTextFill(Color.color(Math.random(), Math.random(), Math.random(), Math.random()));
        text.setFont(Font.font("TimesRomes", FontWeight.BOLD, FontPosture.ITALIC, 22));
        Label text1 = new Label("请输入微信联系人昵称或备注: ");
        TextField text1Field = new TextField("微信联系人(备注)");
        Label text2 = new Label("请输入要发送的文本消息: ");
        TextField text2Field = new TextField("你好!");
        Label text3 = new Label("请输入要发送的次数: ");
        TextField text3Field = new TextField("2");
        Label text4 = new Label("请输入发送时间间隔(毫秒): ");
        TextField text4Field = new TextField("300");
        GridPane textPane = new GridPane();
        textPane.add(text, 0, 0);
        textPane.add(text1, 0, 1);
        textPane.add(text1Field, 2,1);
        textPane.add(text2, 0, 2);
        textPane.add(text2Field, 2,2);
        textPane.add(text3, 0, 3);
        textPane.add(text3Field, 2,3);
        textPane.add(text4, 0, 4);
        textPane.add(text4Field, 2,4);
        textPane.add(send, 1, 5);
        textPane.add(canel, 1, 6);
        send.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //点击后执行的函数
                try {
                    String toName = text1Field.getText();
                    if (StrUtil.isEmpty(toName)) {
                        Alert alert = new Alert(Alert.AlertType.WARNING);
                        alert.setContentText("微信联系人不能为空!");
                        alert.show();
                    }
                    String text = text2Field.getText();
                    if (StrUtil.isEmpty(text)) {
                        Alert alert = new Alert(Alert.AlertType.WARNING);
                        alert.setContentText("发送消息不能为空!");
                        alert.show();
                    }
                    String count = text3Field.getText();
                    if (StrUtil.isEmpty(count)) {
                        Alert alert = new Alert(Alert.AlertType.WARNING);
                        alert.setContentText("发送次数不能为空!");
                        alert.show();
                    }
                    Long countL = Long.valueOf(count);
                    String interval = text4Field.getText();
                    if (StrUtil.isEmpty(interval)) {
                        Alert alert = new Alert(Alert.AlertType.WARNING);
                        alert.setContentText("发送时间间隔不能为空!");
                        alert.show();
                    }
                    Long intervalL = Long.valueOf(interval);
                    RobotUtil.runRobotText(toName, text, countL, intervalL);
                } catch (Exception e) {
                    Alert alert = new Alert(Alert.AlertType.WARNING);
                    alert.setContentText("入参错误!请确认次数和时间是否为数字!");
                    alert.show();
                    throw new RuntimeException(e);
                }
            }
        });
        canel.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                primaryStage.setScene(scene);
                primaryStage.show();
            }
            });
        return textPane;
    }

    public GridPane runImage(Stage primaryStage, Scene scene) {
        Button send = new Button("发送");
        Button canel = new Button("返回");
        Button choose = new Button("选择文件");
        GridPane imagePane = new GridPane();
        //文本框
        Label text = new Label("请确保微信聊天界面处于最上层!!!");
        text.setTextFill(Color.color(Math.random(), Math.random(), Math.random(), Math.random()));
        text.setFont(Font.font("TimesRomes", FontWeight.BOLD, FontPosture.ITALIC, 22));
        Label text1 = new Label("请输入微信联系人昵称或备注: ");
        TextField text1Field = new TextField("微信联系人(备注)");
        Label text2 = new Label("请选择要发送的图片或表情: ");
        TextField text2Field = new TextField();
        //文件选择器
        FileChooser fileChooser = new FileChooser();
        Label text3 = new Label("请输入要发送的次数: ");
        TextField text3Field = new TextField("2");
        Label text4 = new Label("请输入发送时间间隔(毫秒): ");
        TextField text4Field = new TextField("300");
        imagePane.add(text, 0, 0);
        imagePane.add(text1, 0, 1);
        imagePane.add(text1Field, 1,1);
        imagePane.add(text2, 0, 2);
        imagePane.add(text2Field, 1,2);
        imagePane.add(choose, 2,2);
        imagePane.add(text3, 0, 3);
        imagePane.add(text3Field, 1,3);
        imagePane.add(text4, 0, 4);
        imagePane.add(text4Field, 1,4);
        imagePane.add(send, 1, 5);
        imagePane.add(canel, 1, 6);
        send.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //点击后执行的函数
                try {
                    String toName = text1Field.getText();
                    if (StrUtil.isEmpty(toName)) {
                        Alert alert = new Alert(Alert.AlertType.WARNING);
                        alert.setContentText("微信联系人不能为空!");
                        alert.show();
                    }
                    String text = text2Field.getText();
                    if (StrUtil.isEmpty(text)) {
                        Alert alert = new Alert(Alert.AlertType.WARNING);
                        alert.setContentText("发送图片路径不能为空!");
                        alert.show();
                    }
                    String count = text3Field.getText();
                    if (StrUtil.isEmpty(count)) {
                        Alert alert = new Alert(Alert.AlertType.WARNING);
                        alert.setContentText("发送次数不能为空!");
                        alert.show();
                    }
                    Long countL = Long.valueOf(count);
                    String interval = text4Field.getText();
                    if (StrUtil.isEmpty(interval)) {
                        Alert alert = new Alert(Alert.AlertType.WARNING);
                        alert.setContentText("发送时间间隔不能为空!");
                        alert.show();
                    }
                    Long intervalL = Long.valueOf(interval);
                    RobotUtil.runRobotImage(toName, text, countL, intervalL);
                } catch (Exception e) {
                    Alert alert = new Alert(Alert.AlertType.WARNING);
                    alert.setContentText("入参错误!请确认次数和时间是否为数字!");
                    alert.show();
                    throw new RuntimeException(e);
                }
            }
        });
        canel.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                primaryStage.setScene(scene);
                primaryStage.show();
            }
        });
        choose.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                File selectedFile = fileChooser.showOpenDialog(primaryStage);
                text2Field.setText(selectedFile.getPath());
            }
        });
        return imagePane;
    }

    @Override
    public void init() throws Exception {
        super.init();
        System.out.println("------------------------连发器加载中------------------------");
    }

    @Override
    public void stop() throws Exception {
        super.stop();
        System.out.println("------------------------连发器停止运行------------------------");
    }
}
